package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.exception.BizException;
import com.qk.dto.customer.AddCustomerDTO;
import com.qk.dto.customer.QueryPageCustomerDTO;
import com.qk.dto.customer.UpdateCustomerDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author:gzc
 * @Date: 2025/10/06 21:57
 * @Description:
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Override
    public PageResult<CustomerVO> queryListByPage(QueryPageCustomerDTO queryPageCustomerDTO) {
        Page<CustomerVO> page = this.baseMapper.queryListByPage(new Page<>(queryPageCustomerDTO.getPage(), queryPageCustomerDTO.getPageSize()), queryPageCustomerDTO);
        return PageResult.<CustomerVO>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
    }

    @Override
    public void addCustomer(AddCustomerDTO addCustomerDTO) {
        if (ObjectUtil.isEmpty(addCustomerDTO.getPhone())||ObjectUtil.isEmpty(addCustomerDTO.getName())) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(addCustomerDTO, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.save(customer);
    }

    @Override
    public Customer queryById(Integer id) {
        if (ObjectUtil.isEmpty(id)){
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        return this.getById(id);
    }

    @Override
    public void updateCustomer(UpdateCustomerDTO updateCustomerDTO) {
        if (ObjectUtil.isEmpty(updateCustomerDTO.getId())||ObjectUtil.isEmpty(updateCustomerDTO.getName())||ObjectUtil.isEmpty(updateCustomerDTO.getPhone())) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(updateCustomerDTO, Customer.class);
        customer.setUpdateTime(LocalDateTime.now());
        this.updateById(customer);
    }
}
